//
//  BN_DynamicHelper.m
//  BabyNow
//
//  Created by iMobile on 14-4-2.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_DynamicHelper.h"
#import "IMB_NetWorkJsonRespOperation.h"
#import "IMB_NetWorkAccessOperationWithFilesUpload.h"
#import "BN_UserInfo.h"
#import "BN_UserHelper.h"
#import "IMB_NetFileUploadHelper.h"



@interface BN_DynamicHelper()

{
    // 班级动态列表
    NSMutableArray *dataArrForClass_;
    
    // 家长动态列表
    NSMutableArray *dataArrForParent_;
    
    // 当前班级动态列表页码
    int pageNoForClass_;
    
    // 当前家长动态列表页码
    int pageNoForParent_;
    
    // 文件上传任务
    IMB_NetWorkAccessOperationWithFilesUpload *uploadOperation_;
    
    // 上传进度回调处理
    UploadProcessingHandler uploadProcessingHandler_;
    
    /**
     *  文件上传帮助类
     */
    IMB_NetFileUploadHelper *fileUploadHelper_;
}

/**
 *  班级动态列表查询队列
 */
@property (nonatomic,strong) NSOperationQueue *queryClassListQueue;

/**
 *  家长动态列表查询队列
 */
@property (nonatomic,strong) NSOperationQueue *queryParentListQueue;

/**
 *  详情查询队列
 */
@property (nonatomic,strong) NSOperationQueue *queryDetailQueue;

/**
 *  动态回复队列
 */
@property (nonatomic,strong) NSOperationQueue *postReplContQueue;

/**
 *  动态发布队列
 */
@property (nonatomic,strong) NSOperationQueue *postDynamicQueue;

/**
 *  文件上传队列
 */
@property (nonatomic,strong) NSOperationQueue *uploadFileQueue;

/**
 *  动态移除队列
 */
@property (nonatomic,strong) NSOperationQueue *removeDynamicQueue;

/**
 * 动态赞队列
 */
@property (nonatomic,strong) NSOperationQueue *supportDynamicQueue;

/**
 *  动态收藏队列
 */
@property (nonatomic,strong) NSOperationQueue *collectDynamicQueue;


/**
 *  检测是否可以请求班级动态列表数据
 *
 *  @return 检测结果
 */
- (ENUM_CAN_NET_REQUEST_OR_NOT) canRequestDynamicListForClass;

/**
 *  检测是否可以请求家长动态列表数据
 *
 *  @return 检测结果
 */
- (ENUM_CAN_NET_REQUEST_OR_NOT) canRequestDynamicListForParent;

/**
 *  查询指定页码的班级动态数据
 *
 *  @param pageNo 页码
 */
- (void)queryDynamicListForClass:(int)pageNo;

/**
 *  查询指定页码的家长动态数据
 *
 *  @param pageNo 页码
 */
- (void)queryDynamicListForParent:(int)pageNo;

// add by jason yan 2014-07-26

// 查询指定家长动态页码数据新接口
- (void)newQueryDynamicListForParent:(int)pageNo;

@end

@implementation BN_DynamicHelper

#pragma mark - Extend method

// 检测是否可以请求班级动态列表数据
- (ENUM_CAN_NET_REQUEST_OR_NOT)canRequestDynamicListForClass{
    
    if (!self.queryClassListCompleteHandler) {
        return ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST;
    }
    if (self.queryClassListQueue.operationCount > 0) {
        return ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED;
    }
    if (![IMB_NetUtils isNetConnect]) {
        return ENUM_CANNOT_NET_REQUEST_FOR_WIFI_OR_3G_NOT_ACCESS;
    }
    return ENUM_CAN_NET_REQUEST;
}

// 检测是否可以请求家长动态列表数据
- (ENUM_CAN_NET_REQUEST_OR_NOT)canRequestDynamicListForParent{
    if (!self.queryParentListCompleteHandler) {
        return ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST;
    }
    if (self.queryParentListQueue.operationCount > 0) {
        return ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED;
    }
    if (![IMB_NetUtils isNetConnect]) {
        return ENUM_CANNOT_NET_REQUEST_FOR_WIFI_OR_3G_NOT_ACCESS;
    }
    return ENUM_CAN_NET_REQUEST;
}

// 查询指定班级动态页码数据
- (void) queryDynamicListForClass:(int)pageNo{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kPostType:[NSString stringWithFormat:@"%d",ENUM_DYNAIMC_FOR_CLASS],kPageno: [NSString stringWithFormat:@"%d",pageNo]} completeHandler:^(id result, NSString *errorMessage) {
        
        /*if (errorMessage) {
            self.queryClassListCompleteHandler(nil,errorMessage);
        }else{*/
        
        // update by jason yan 2014-05-30
        if (result) {
            NSLog(@"%@",NET_IF_DYNAMIC_LIST);
            // 检查errorCode的值
            if ([result[kErrorCode]intValue] == 0) {
                if (pageNo == 1) {
                    [dataArrForClass_ removeAllObjects];
                }
                NSArray *arr = result[kinfo];
                if (arr.count > 0) {
                    pageNoForClass_ = pageNo;
                    
                    //update by jason yan 2014-07-03 target:modifiy value of the dict
                    //[dataArrForClass_ addObjectsFromArray:arr];
                    for (NSDictionary *dict in arr) {
                        [dataArrForClass_ addObject:[dict mutableCopy]];
                    }
                    self.queryClassListCompleteHandler(dataArrForClass_,errorMessage);
                }else{
                    if (pageNo == 1) {
                        self.queryClassListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.queryClassListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.queryClassListCompleteHandler(nil,result[kErrorMessage]);
            }
        }else{
            self.queryClassListCompleteHandler(nil,errorMessage);
        }
        
        //}
    }];
    [self.queryClassListQueue addOperation:operation];
}

// 查询指定家长动态页码数据新接口
- (void)newQueryDynamicListForParent:(int)pageNo{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
#warning 测试数据
    
    NSString *userId = TEST_USER_ID_PARENT_1;
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NEW_NET_IF_DOMAIN requestMethod:NET_REQUEST_GET paramsValues:@{kA:NEW_NET_IF_DYNAMIC_LIST,kParams:[@{kUserId:userId,kDynamicType:[NSString stringWithFormat:@"%d",ENUM_DYNAIMC_FOR_PARENT],kPageNo: [NSString stringWithFormat:@"%d",pageNo]} toJsonString]} completeHandler:^(id result, NSString *errorMessage) {
        
        // update by jason yan 2014-05-30
        if (result) {
            /*if (errorMessage) {
             self.queryParentListCompleteHandler(nil,errorMessage);
             }else{*/
            // 检查errorCode的值
            if ([result[kErrorCode]intValue] == 0) {
                if (pageNo == 1) {
                    [dataArrForParent_ removeAllObjects];
                }
                NSArray *arr = result[kinfo];
                if (arr.count > 0) {
                    pageNoForParent_ = pageNo;
                    //update by jason yan 2014-07-03 target:modifiy value of the dict
                    // [dataArrForParent_ addObjectsFromArray:arr];
                    for (NSDictionary *dict in arr) {
                        [dataArrForParent_ addObject:[dict mutableCopy]];
                    }
                    self.queryParentListCompleteHandler(dataArrForParent_,errorMessage);
                }else{
                    //self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    // update by jason yan 2014-06-19
                    if (pageNo == 1) {
                        self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.queryParentListCompleteHandler(nil,result[kErrorMessage]);
            }
        }else{
            self.queryParentListCompleteHandler(nil,errorMessage);
        }
    }];
    [self.queryParentListQueue addOperation:operation];
}

// 查询指定家长动态页码数据
- (void)queryDynamicListForParent:(int)pageNo{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kPostType:[NSString stringWithFormat:@"%d",ENUM_DYNAIMC_FOR_PARENT],kPageno: [NSString stringWithFormat:@"%d",pageNo]} completeHandler:^(id result, NSString *errorMessage) {
        
        // update by jason yan 2014-05-30
        if (result) {
        /*if (errorMessage) {
            self.queryParentListCompleteHandler(nil,errorMessage);
        }else{*/
            // 检查errorCode的值
            if ([result[kErrorCode]intValue] == 0) {
                if (pageNo == 1) {
                    [dataArrForParent_ removeAllObjects];
                }
                NSArray *arr = result[kinfo];
                if (arr.count > 0) {
                    pageNoForParent_ = pageNo;
                    //update by jason yan 2014-07-03 target:modifiy value of the dict
                    // [dataArrForParent_ addObjectsFromArray:arr];
                    for (NSDictionary *dict in arr) {
                        [dataArrForParent_ addObject:[dict mutableCopy]];
                    }
                    self.queryParentListCompleteHandler(dataArrForParent_,errorMessage);
                }else{
                    //self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    // update by jason yan 2014-06-19
                    if (pageNo == 1) {
                        self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.queryParentListCompleteHandler(nil,result[kErrorMessage]);
            }
        }else{
            self.queryParentListCompleteHandler(nil,errorMessage);
        }
    }];
    [self.queryParentListQueue addOperation:operation];
}

#pragma mark - Interface method

// 根据动态编号获取动态详情
- (void)queryDetailWithPostId:(NSString *)postId
              completeHandler:(DataHandleComplete)handler{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    NSLog(@"account:%@,password:%@,postId:%@",userInfo.account,userInfo.password,postId);
    
    NSLog(@"%@",NET_IF_DYNAMIC_DETAIL);
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_DETAIL requestMethod:NET_REQUEST_GET paramsValues:@{kAccount: userInfo.account,kPassword:userInfo.password,kPostId:postId} completeHandler:^(id result, NSString *errorMessage) {
        
        // update by jason yan 2014-05-30
        
        if (result) {
        /*if (errorMessage) {
            handler(nil,errorMessage);
        }else{*/
            // 检查errorCode的值
            if ([result[kErrorCode] intValue] == 0) {
                if(result[kinfo][0]){
                    handler(result[kinfo][0],errorMessage);
                }else{
                    handler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                }                
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }else{
            handler(nil,errorMessage);
        }
    }];
    [self.queryDetailQueue addOperation:operation];
}

// 动态回复
- (void)postReplyWithPostId:(NSString *)postId
                   replCont:(NSString *)replCont
            compelteHandler:(DataHandleComplete)handler{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_REPLY requestMethod:NET_REQUEST_POST paramsValues:@{kAccount: userInfo.account,kPassword:userInfo.password,kPostId:postId,kReplCont:replCont} completeHandler:^(id result, NSString *errorMessage) {
        
        if (errorMessage) {
            handler(nil,errorMessage);
        }else{
            if ([result[kErrorCode]intValue] == 0) {
                handler(result[kErrorMessage],nil);
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }
    }];
    [self.postReplContQueue addOperation:operation];
}

- (void)publishDynamicWithSubj:(NSString*)subj
                       fileIds:(NSString*)fileIds
                      userInfo:(BN_UserInfo*)userInfo
               completeHandler:(DataHandleComplete)handler{
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:NET_IF_DYNAMIC_PUBLISH]];
    NSLog(@"%@",[NSURL URLWithString:NET_IF_DYNAMIC_PUBLISH]);
    
    request.HTTPMethod = NET_REQUEST_POST;

    NSDictionary *paramsDict = @{kAccount: userInfo.account,kPassword:userInfo.password,kPostSubj:subj,kFileIds:fileIds};
    
    NSMutableString *paramsStr = [NSMutableString string];
    if (paramsDict) {
        for (NSString *key in paramsDict) {
            [paramsStr appendString:key];
            [paramsStr appendString:@"="];
            [paramsStr appendString:paramsDict[key]];
            [paramsStr appendString:@"&"];
        }
        [paramsStr deleteCharactersInRange:NSMakeRange(paramsStr.length - 1, 1)];
    }
    request.timeoutInterval = NET_REQUEST_TIME_OUT;
    request.HTTPBody = [paramsStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSHTTPURLResponse *response;
    
    NSError *error;
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"发布动态信息出错:%@",error);
        NSString *errorMsg;
        if (response.statusCode == ENUM_HTTP_STATUS_OK) {
            errorMsg =  CONNECTION_ERROR_MSG_FOR_TIME_OUT;
        }else{
            NSInteger statusCode = response.statusCode;
            if (statusCode > ENUM_HTTP_STATUS_NET_IF_SERVER_EXCEPTION) {
                errorMsg = NET_ERROR_MSG_FOR_NET_IF_EXCEPTION;
            }
            switch (statusCode) {
                case ENUM_HTTP_STATUS_NET_IF_REQUEST_METHOD_ERROR:
                    errorMsg =  NET_ERROR_MSG_FOR_NET_IF_REQUEST_METHOD_ERROR;
                    break;
                case ENUM_HTTP_STATUS_NET_IF_NOT_EXIST:
                    errorMsg =  NET_ERROR_MSG_FOR_NET_IF_NOT_EXIST;
                    break;
                case ENUM_HTTP_STATUS_NET_IF_NOT_IMP:
                    errorMsg =  NET_ERROR_MSG_FOR_NET_IF_NOT_IMP;
                    break;
                default:
                    NSLog(@"###### 未知statusCode:%d",statusCode);
                    errorMsg =  NET_ERROR_MSG_FOR_HTTP_STATUS_UNKNOW;
                    break;
            }
        }
        handler(nil,errorMsg);
    }else{
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSLog(@"发布动态结果：%@",result);
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                handler(result[kErrorMessage],nil);
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }else{
            handler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
        }
    }
}


// 动态发布
- (void)postDynamicWithSubj:(NSString *)sub postType:(ENUM_DYNAIMC_TYPE)dynamicType filesInfoArr:(NSArray *)fileInfoArr completeHandler:(DataHandleComplete)completehandler uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler{
    
    if (self.uploadFileQueue.operationCount > 0) {
        return;
    }
    
    uploadProcessingHandler_ = uploadProcessHandler;
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_PUBLISH requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        NSLog(@"发布动态结果：%@",result);
        if (errorMessage) {
            completehandler(nil,errorMessage);
        }else{
            if ([result[kErrorCode]intValue] == 0) {
                completehandler(result[kErrorMessage],nil);
            }else{
                completehandler(nil,result[kErrorMessage]);
            }
        }
    }];
    
    // 1. 执行文件上传操作
    uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoArr completeHandler:^(id result, NSString *errorMessage) {
        
        [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
        
        if (errorMessage) {
            [self.uploadFileQueue cancelAllOperations];
            completehandler(nil,errorMessage);
        }else if(result){
            NSError *err;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
            if (err) {
                completehandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
            }else{
                NSLog(@"文件上传结果：%@",dict);
                // 检查errorCode
                if ([dict[kErrorCode]intValue] == 0) {
                    //handler(dict[kinfo],nil);
                    
                    NSMutableString *fileIdsStr = [NSMutableString string];
                    NSArray *infoArr = dict[kinfo];
                    for (NSDictionary *fileDict in infoArr) {
                        [fileIdsStr appendString:fileDict[kFileId]];
                        [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                    }
                    NSLog(@"fileIds:%@",fileIdsStr);
                    NSLog(@"动态发布地址：%@",NET_IF_DYNAMIC_PUBLISH);
                    
                    operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kPostSubj:sub,kFileIds:fileIdsStr,kPostType:[NSString stringWithFormat:@"%d",dynamicType]};
                    
                    //[self publishDynamicWithSubj:sub fileIds:fileIdsStr userInfo:userInfo completeHandler:completehandler];
                }else{
                    completehandler(nil,dict[kErrorMessage]);
                }
            }
        }
    }];
    
    [operation addDependency:uploadOperation_];
    // 添加观察者
    [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
    [self.uploadFileQueue addOperation:operation];
    [self.uploadFileQueue addOperation:uploadOperation_];
    
}

// 新动态发布接口
- (void)newPostDynamicWithContent:(NSString *)content filesInfoArr:(NSArray *)fileInfoArr completeHandler:(DataHandleComplete)completehandler uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler{

    // BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
#warning 测试用户编号
    NSString *userId = TEST_USER_ID_PARENT_1;
    
    fileUploadHelper_ = [[IMB_NetFileUploadHelper alloc]initWithUrlStr:NEW_NET_IF_PUBLISH_DYNAMIC commonFormParamsDict:@{kUserId:userId,kContent:content} fileInfoArr:fileInfoArr completeHandler:completehandler uploadProcessingHandler:uploadProcessHandler];
    
}


// 动态发布
- (void)postDynamicWithSubj:(NSString*)sub
                   postType:(ENUM_DYNAIMC_TYPE)dynamicType
               fileMimeType:(NSString*)fileMimeType
              filesInfoDict:(NSDictionary*)fileInfoDict
            completeHandler:(DataHandleComplete)completehandler
    uploadProcessingHandler:(UploadProcessingHandler)uploadProcessHandler{
    
    if (self.uploadFileQueue.operationCount > 0) {
        return;
    }
    
    uploadProcessingHandler_ = uploadProcessHandler;
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];

    NSLog(@"%@",NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE);
    
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_PUBLISH requestMethod:NET_REQUEST_POST paramsValues:nil completeHandler:^(id result, NSString *errorMessage) {
        NSLog(@"发布动态结果：%@",result);
        if (errorMessage) {
            completehandler(nil,errorMessage);
        }else{
            if ([result[kErrorCode]intValue] == 0) {
                completehandler(result[kErrorMessage],nil);
            }else{
                completehandler(nil,result[kErrorMessage]);
            }
        }
    }];
    
    // 1. 执行文件上传操作
    uploadOperation_ = [[IMB_NetWorkAccessOperationWithFilesUpload alloc]initWithUploadUrlStr:NET_IF_DYNAMIC_PUBLISH_UPLOAD_FILE paramsValues:@{kAccount: userInfo.account} paramsFileName:UPLOAD_FILE_PARAM_NAME paramsFileData:fileInfoDict fileMimeType:fileMimeType completeHandler:^(id result, NSString *errorMessage) {
        
        [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
        
        if (errorMessage) {
            completehandler(nil,errorMessage);
        }else if(result){
            NSError *err;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&err];
            if (err) {
                completehandler(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
            }else{
                NSLog(@"文件上传结果：%@",dict);
                // 检查errorCode
                if ([dict[kErrorCode]intValue] == 0) {
                    //handler(dict[kinfo],nil);
                    
                    NSMutableString *fileIdsStr = [NSMutableString string];
                    NSArray *infoArr = dict[kinfo];
                    for (NSDictionary *fileDict in infoArr) {
                        [fileIdsStr appendString:fileDict[kFileId]];
                        [fileIdsStr appendString:SEPERATOR_FOR_FILE_IDS];
                    }
                    NSLog(@"fileIds:%@",fileIdsStr);
                    NSLog(@"动态发布地址：%@",NET_IF_DYNAMIC_PUBLISH);
                    
                    operation -> paramsDict_ = @{kAccount: userInfo.account,kPassword:userInfo.password,kPostSubj:sub,kFileIds:fileIdsStr,kPostType:[NSString stringWithFormat:@"%d",dynamicType]};
                    
                    //[self publishDynamicWithSubj:sub fileIds:fileIdsStr userInfo:userInfo completeHandler:completehandler];
                }else{
                    completehandler(nil,dict[kErrorMessage]);
                }
            }
        }
    }];
    
    [operation addDependency:uploadOperation_];
    // 添加观察者
    [uploadOperation_ addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:NULL];
    [self.uploadFileQueue addOperation:operation];
    [self.uploadFileQueue addOperation:uploadOperation_];
}






// 移除动态信息
- (void)removeDynamicWithPostId:(NSString *)postId
                completeHandler:(DataHandleComplete)handler{

    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_DELETE requestMethod:NET_REQUEST_POST paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kPostId:postId} completeHandler:^(id result, NSString *errorMessage) {
        
        if (errorMessage) {
            handler(nil,errorMessage);
        }else{
            if (result[kErrorCode] == 0) {
                handler(result[kErrorMessage],nil);
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }
    }];
    
    [self.removeDynamicQueue addOperation:operation];

}

// 点赞动态
- (void)supportDynamicWithPostId:(NSString *)postId
                             flg:(int)flg
                 completeHandler:(DataHandleComplete)handler{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_SUPPORT requestMethod:NET_REQUEST_POST paramsValues:@{kAccount: userInfo.account,kPassword:userInfo.password,kPostId:postId,kFlg:[NSString stringWithFormat:@"%d",flg]} completeHandler:^(id result, NSString *errorMessage) {
        if (errorMessage) {
            handler(nil,errorMessage);
        }else{
            // 检查errorCode
            if ([result[kErrorCode]intValue] == 0) {
                handler(result[kErrorMessage],nil);
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }
    }];
    [self.supportDynamicQueue addOperation:operation];
}

// 收藏动态
- (void)collectDynamicWithPostId:(NSString *)postId
                 completeHandler:(DataHandleComplete)handler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_DYNAMIC_COLLECT requestMethod:NET_REQUEST_POST paramsValues:@{kAccount: userInfo.account,kPassword:userInfo.password,kPostId:postId} completeHandler:^(id result, NSString *errorMessage) {
        if (errorMessage) {
            handler(nil,errorMessage);
        }else{
            // 检查errorCode
            if ([result[kErrorCode]intValue] == 0) {
                handler(result[kErrorMessage],nil);
            }else{
                handler(nil,result[kErrorMessage]);
            }
        }
    }];
    [self.collectDynamicQueue addOperation:operation];
}


// 刷新班级动态列表
- (void)refreshClassList{
    switch ([self canRequestDynamicListForClass]) {
        case ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST:
            NSAssert(self.queryClassListQueue, @"没有初始化班级动态列表请求处理器对象");
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED:
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_SERVER_NOT_ACCESS:
            self.queryClassListCompleteHandler(nil,NET_ERROR_MSG_FOR_NET_NOT_ACCESS);
            return;
            break;
        default:
            break;
    }
    
    // 查询第一页数据
    [self queryDynamicListForClass:1];
}

// 刷新家长动态列表
- (void)refreshParentList{
    switch ([self canRequestDynamicListForParent]) {
        case ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST:
            NSAssert(self.queryClassListQueue, @"没有初始化家长动态列表请求处理器对象");
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED:
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_SERVER_NOT_ACCESS:
            self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NET_NOT_ACCESS);
            return;
            break;
        default:
            break;
    }
    
    // 查询第一页数据
    [self queryDynamicListForParent:1];
}

// 刷新家长动态列表新接口
- (void)newRefreshParentList{
    switch ([self canRequestDynamicListForParent]) {
        case ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST:
            NSAssert(self.queryClassListQueue, @"没有初始化家长动态列表请求处理器对象");
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED:
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_SERVER_NOT_ACCESS:
            self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NET_NOT_ACCESS);
            return;
            break;
        default:
            break;
    }
    
    // 查询第一页数据
    [self newQueryDynamicListForParent:1];
}

// 获取下一页班级动态列表
- (void)fetchNextPageForClass{
    
    switch ([self canRequestDynamicListForClass]) {
        case ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST:
            NSAssert(self.queryClassListCompleteHandler, @"没有初始化班级动态列表请求处理器对象");
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED:
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_SERVER_NOT_ACCESS:
            self.queryClassListCompleteHandler(nil,NET_ERROR_MSG_FOR_NET_NOT_ACCESS);
            return;
            break;
        default:
            break;
    }
    
    // 查询下一页数据
    [self queryDynamicListForClass:pageNoForClass_ + 1];
}

// 获取下一页家长动态列表
- (void)fetchNextPageForParent{
    switch ([self canRequestDynamicListForParent]) {
        case ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST:
            NSAssert(self.queryClassListCompleteHandler, @"没有初始化家长动态列表请求处理器对象");
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED:
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_SERVER_NOT_ACCESS:
            self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NET_NOT_ACCESS);
            return;
            break;
        default:
            break;
    }
    
    // 查询下一页数据
    [self queryDynamicListForParent:pageNoForParent_ + 1];
}

// 获取下一页家长动态列表
- (void)newFetchNextPageForParent{
    switch ([self canRequestDynamicListForParent]) {
        case ENUM_CANNOT_NET_REQUEST_FOR_DATA_HANDLER_NOT_EXIST:
            NSAssert(self.queryClassListCompleteHandler, @"没有初始化家长动态列表请求处理器对象");
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_PREV_REQUEST_NOT_FINISHED:
            return;
            break;
        case ENUM_CANNOT_NET_REQUEST_FOR_SERVER_NOT_ACCESS:
            self.queryParentListCompleteHandler(nil,NET_ERROR_MSG_FOR_NET_NOT_ACCESS);
            return;
            break;
        default:
            break;
    }
    
    // 查询下一页数据
    [self newQueryDynamicListForParent:pageNoForParent_ + 1];
}

// 取消查询班级动态列表操作
- (void)cancelQueryListForClass{
    [self.queryClassListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消查询家长动态列表操作
- (void)cancelQueryListForParent{
    [self.queryParentListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消查询动态详情数据
- (void)cancelQueryDetailInfo{
    [self.queryDetailQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消发布动态信息操作
- (void)cancelPostDynamicInfo{
    [self.postDynamicQueue.operations makeObjectsPerformSelector:@selector(cancelUpload)];
}

#pragma mark - Property method

// 获取班级动态列表查询队列
- (NSOperationQueue*)queryClassListQueue{
    if (!_queryClassListQueue) {
        _queryClassListQueue = [[NSOperationQueue alloc]init];
    }
    return _queryClassListQueue;
}

// 获取家长动态列表查询队列
- (NSOperationQueue*)queryParentListQueue{
    if (!_queryParentListQueue) {
        _queryParentListQueue = [[NSOperationQueue alloc]init];
    }
    return _queryParentListQueue;
}

// 获取详情查询队列
- (NSOperationQueue*)queryDetailQueue{
    if (!_queryDetailQueue) {
        _queryDetailQueue = [[NSOperationQueue alloc]init];
    }
    return _queryDetailQueue;
}

// 动态回复队列
- (NSOperationQueue*)postReplContQueue{
    if (!_postReplContQueue) {
        _postReplContQueue = [[NSOperationQueue alloc]init];
    }
    return _postReplContQueue;
}

// 动态发布队列
- (NSOperationQueue*)postDynamicQueue{
    if (!_postReplContQueue) {
        _postReplContQueue = [[NSOperationQueue alloc]init];
    }
    return _postDynamicQueue;
}

// 文件上传队列
- (NSOperationQueue*)uploadFileQueue{
    if (!_uploadFileQueue) {
        _uploadFileQueue = [[NSOperationQueue alloc]init];
    }
    return _uploadFileQueue;
}

// 移除动态队列
- (NSOperationQueue*)removeDynamicQueue{
    if (!_removeDynamicQueue) {
        _removeDynamicQueue = [[NSOperationQueue alloc]init];
    }
    return _removeDynamicQueue;
}

// 赞动态队列
- (NSOperationQueue*)supportDynamicQueue{
    if (!_supportDynamicQueue) {
        _supportDynamicQueue = [[NSOperationQueue alloc]init];
    }
    return _supportDynamicQueue;
}

// 收藏动态队列
- (NSOperationQueue*)collectDynamicQueue{
    if (!_collectDynamicQueue) {
        _collectDynamicQueue = [[NSOperationQueue alloc]init];
    }
    return _collectDynamicQueue;
}


#pragma mark - LifeCycle method

- (void)dealloc{
    // NSLog(@"BN_DynamicHelper dealloc....");
    if (uploadOperation_) {
        [uploadOperation_ removeObserver:self forKeyPath:@"progress"];
    }
}

- (id)init{
    if (self = [super init]) {
        dataArrForClass_ = [NSMutableArray array];
        dataArrForParent_ = [NSMutableArray array];
    }
    return self;
}

// 观察者回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"progress"]) {
        IMB_NetWorkAccessOperationWithFilesUpload *operation = object;
        uploadProcessingHandler_(operation.progress);
    }
}

@end
